<template>
  <div class="login"
       style="height: 100vh;  display: flex; align-items: center; justify-content: center; background-image: url('https://www.dmoe.cc/random.php'); background-size: cover ">
    <div style="margin-right: -700px; background: rgba(255, 255, 255, 0.6); /* 半透明背景 */margin-right: 100px; border-radius: 20px; ">
      <div style="display: flex; width: 450px; height: 400px; align-items: center; justify-content: center;">
        <el-form :model="user" :rules="rules" style="width: 70%;" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 20px; text-align: center;">欢迎注册</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" prefix-icon="el-icon-s-promotion" placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="password1">
            <el-input type="password" prefix-icon="el-icon-s-promotion" placeholder="请确认密码" v-model="user.password1"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="user.role">
            <el-radio label="用户"></el-radio>
            <el-radio label="商家"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-from-itme>
            <el-button style="width: 100%;" type="primary" @click.native="login11">注册</el-button>
          </el-from-itme>
          <div style="display: flex;">
            <div style="font-size: 14px; flex: 1; margin-top: 10px;">还没有账号？请 <span
                style="color: rgb(55, 222, 222)" @click="$router.push('/')">登录</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>

import ValidCode from "../components/ValidCode.vue";
import request from "@/utils/request";
export default {
  name: "LoginView",
  components: {
    ValidCode
  },
  data() {
    var validatePassword = (rule, password1, callback) => {
      if (password1 === '') {
        callback(new Error('请再次输入密码'));
      } else if (password1 !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user: {
        username: '',
        password: '',
        password1: '',
      },
      register: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        password1: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    getCode(code) {
      console.log(code)
      this.code = code.toLowerCase()
    },
    login11() {
      this.$refs['loginRef'].validate(valid => {
        if (valid) {
          // 验证通过发起请求
          request.post('/login/ZhuCe', this.user).then(res => {
            if (res.code === 200) {
              // 登录成功
              // 1. 保存token
              localStorage.setItem('token', res.data.token)
              // 2. 跳转到登录页面
              this.$router.push('/')
              this.$message.success('注册成功')
            } else {
              // 登录失败
              this.$message.error(res.message)
            }
          })
        }
      })
    },
  }
}

</script>

<style></style>